package com.xquant.marketdata.curvestrip.entity;

import com.xquant.marketdata.utils.ExcelAttribute;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.*;


/**
 * @Author: xpp -_- -_-
 * @Date: 2021/3/22 13:31
 * 违约率报价导入  协会模板实体类
 */
public class TdailyQuoteAssociation implements IDailyQuote {

    public final static int MODEL_TYPE = 0;



    @ExcelAttribute(name = "发布日期")
    private String begDate;
    @ExcelAttribute(name = "编码")
    private String iCode;
    @ExcelAttribute(name = "参考实体")
    private String iName;
    @ExcelAttribute(name = "期限")
    private String term;
    @ExcelAttribute(name = "价格类型")
    private String dpType;
    @ExcelAttribute(name = "价格(BP)")
    private String dpValue;
    //报卖价
    private   BigDecimal dpAsk;
    //报买价
    private   BigDecimal dpBid;
    //中间价
    private   BigDecimal dpMid;

    private final static String DpType_dpAsk = "卖价";
    private final static String DpType_dpBid = "买价";
    private final static String DpType_dpMid = "均价";
    public final static List<String> titleList = Arrays.asList("序号","发布日期","编码","参考实体","期限","价格类型","价格(BP)");

    public String getBegDate() {
        return begDate;
    }

    public void setBegDate(String begDate) {
        this.begDate = begDate;
    }

    public String getiCode() {
        return iCode;
    }

    public void setiCode(String iCode) {
        this.iCode = iCode;
    }

    public String getiName() {
        return iName;
    }

    public void setiName(String iName) {
        this.iName = iName;
    }

    public String getTerm() {
        return term;
    }

    public void setTerm(String term) {
        this.term = term;
    }

    public String getDpType() {
        return dpType;
    }

    public void setDpType(String dpType) {
        this.dpType = dpType;
    }

    public String getDpValue() {
        return StringUtils.isNotBlank(this.dpValue)?this.dpValue:"0";
    }

    public void setDpValue(String dpValue) {
        this.dpValue = dpValue;
    }

    public  BigDecimal getDpAsk() {
        return dpAsk;
    }

    public void setDpAsk(  BigDecimal dpAsk) {
        this.dpAsk = dpAsk;
    }

    public   BigDecimal getDpBid() {
        return dpBid;
    }

    public void setDpBid(  BigDecimal dpBid) {
        this.dpBid = dpBid;
    }

    public   BigDecimal getDpMid() {
        return dpMid;
    }

    public void setDpMid(  BigDecimal dpMid) {
        this.dpMid = dpMid;
    }

    public TdailyQuoteAssociation() {
    }

    @Override
    public String toString() {
        return "tdailyQuoteAssociation{" +
                "iCode='" + iCode + '\'' +
                ", iName='" + iName + '\'' +
                ", term='" + term + '\'' +
                ", dpType='" + dpType + '\'' +
                ", dpValue='" + this.getDpValue() + '\'' +
                ", dpAsk=" + dpAsk +
                ", dpBid=" + dpBid +
                ", dpMid=" + dpMid +
                '}';
    }


    public static List<TdailyQuoteAssociation> resolveAssociations(List<TdailyQuoteAssociation> datas){
        Map<String, TdailyQuoteAssociation> map = new WeakHashMap<>();
        for (TdailyQuoteAssociation association : datas){
            String s = association.getiCode();
            String key = MessageFormat.format("{0}_{1}",s.substring(0, s.length() - 1),association.getBegDate());
            if(map.get(key) == null) {
                map.put(key, association);
            }
            switch (association.getDpType()){
                case DpType_dpAsk:
                    map.get(key).setDpAsk(new BigDecimal(association.getDpValue()).divide(new BigDecimal("10000")));
                    continue;
                case DpType_dpBid:
                    map.get(key).setDpBid(new BigDecimal(association.getDpValue()).divide(new BigDecimal("10000")));
                    continue;
                default:
                    map.get(key).setDpMid(new BigDecimal(association.getDpValue()).divide(new BigDecimal("10000")));
                    continue;
            }
        }
        return new ArrayList<TdailyQuoteAssociation>(map.values());
    }
}
